package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.common.pojo.User;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.api.QuestionApi;
import com.tanhua.dubbo.server.api.SoulApi;
import com.tanhua.dubbo.server.pojo.Question;
import com.tanhua.dubbo.server.pojo.Soul;
import com.tanhua.server.vo.SoulVo;

import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SoulService {

    @Reference(version = "1.0.0")
    private SoulApi soulApi;

    @Reference(version = "1.0.0")
    private QuestionApi questionApi;


    public Object[] getSoulVoList() {
        User user = UserThreadLocal.get();
        List<Soul> soulList = soulApi.findByUserId(user.getId());

        //如果mongodb中没有查询到即为第一次来答题，需要初始化题目
        if(CollUtil.isEmpty(soulList)){
            soulApi.saveSoul(user.getId(),"初级灵魂题","https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/test_soul/qn_cover_01.png",
                    "初级",2,1,0);
            soulApi.saveSoul(user.getId(),"中级灵魂题","https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/test_soul/qn_cover_02.png",
                    "中级",3,2,1);
            soulApi.saveSoul(user.getId(),"高级灵魂题","https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/test_soul/qn_cover_03.png",
                    "高级",5,3,1);
            soulList = soulApi.findByUserId(user.getId());
        }

        Object[] arr = new Object[soulList.size()];


        for (int i = 0; i < soulList.size(); i++) {
                SoulVo soulVo = new SoulVo();
                soulVo.setId(soulList.get(i).getId().toHexString());
                soulVo.setName(soulList.get(i).getName());
                soulVo.setCover(soulList.get(i).getCover());
                soulVo.setLevel(soulList.get(i).getLevel());
                soulVo.setStar(soulList.get(i).getStar());
                soulVo.setIsLock(soulList.get(i).getIsLock());
                soulVo.setReportId(soulList.get(i).getReportId());

                List<Question> questions = questionApi.queryQuestionListByIds(soulList.get(i).getQuestionIds());

                soulVo.setQuestions(questions.toArray());

                arr[i] = soulVo;
            }

        return arr;
    }

}
